home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / OldSrc / CH9 / SRC / REFLECT.FRM (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-11-16  |  12.9 KB  |  430 lines

  1. VERSION 4.00
  2. Begin VB.Form ReflectForm 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00C0C0C0&
  5.    Caption         =   "Reflection Across a Plane"
  6.    ClientHeight    =   5415
  7.    ClientLeft      =   300
  8.    ClientTop       =   1020
  9.    ClientWidth     =   9015
  10.    BeginProperty Font 
  11.       name            =   "MS Sans Serif"
  12.       charset         =   1
  13.       weight          =   700
  14.       size            =   8.25
  15.       underline       =   0   'False
  16.       italic          =   0   'False
  17.       strikethrough   =   0   'False
  18.    EndProperty
  19.    ForeColor       =   &H80000008&
  20.    Height          =   6105
  21.    KeyPreview      =   -1  'True
  22.    Left            =   240
  23.    LinkTopic       =   "Form1"
  24.    ScaleHeight     =   5415
  25.    ScaleWidth      =   9015
  26.    Top             =   390
  27.    Width           =   9135
  28.    Begin VB.PictureBox Pict 
  29.       AutoRedraw      =   -1  'True
  30.       Height          =   2175
  31.       Index           =   7
  32.       Left            =   6840
  33.       ScaleHeight     =   -10
  34.       ScaleLeft       =   -5
  35.       ScaleMode       =   0  'User
  36.       ScaleTop        =   5
  37.       ScaleWidth      =   10
  38.       TabIndex        =   7
  39.       Top             =   2760
  40.       Width           =   2175
  41.    End
  42.    Begin VB.PictureBox Pict 
  43.       AutoRedraw      =   -1  'True
  44.       Height          =   2175
  45.       Index           =   6
  46.       Left            =   4560
  47.       ScaleHeight     =   -10
  48.       ScaleLeft       =   -5
  49.       ScaleMode       =   0  'User
  50.       ScaleTop        =   5
  51.       ScaleWidth      =   10
  52.       TabIndex        =   6
  53.       Top             =   2760
  54.       Width           =   2175
  55.    End
  56.    Begin VB.PictureBox Pict 
  57.       AutoRedraw      =   -1  'True
  58.       Height          =   2175
  59.       Index           =   5
  60.       Left            =   2280
  61.       ScaleHeight     =   -10
  62.       ScaleLeft       =   -5
  63.       ScaleMode       =   0  'User
  64.       ScaleTop        =   5
  65.       ScaleWidth      =   10
  66.       TabIndex        =   5
  67.       Top             =   2760
  68.       Width           =   2175
  69.    End
  70.    Begin VB.PictureBox Pict 
  71.       AutoRedraw      =   -1  'True
  72.       Height          =   2175
  73.       Index           =   4
  74.       Left            =   0
  75.       ScaleHeight     =   -10
  76.       ScaleLeft       =   -5
  77.       ScaleMode       =   0  'User
  78.       ScaleTop        =   5
  79.       ScaleWidth      =   10
  80.       TabIndex        =   4
  81.       Top             =   2760
  82.       Width           =   2175
  83.    End
  84.    Begin VB.PictureBox Pict 
  85.       AutoRedraw      =   -1  'True
  86.       Height          =   2175
  87.       Index           =   3
  88.       Left            =   6840
  89.       ScaleHeight     =   -10
  90.       ScaleLeft       =   -5
  91.       ScaleMode       =   0  'User
  92.       ScaleTop        =   5
  93.       ScaleWidth      =   10
  94.       TabIndex        =   3
  95.       Top             =   0
  96.       Width           =   2175
  97.    End
  98.    Begin VB.PictureBox Pict 
  99.       AutoRedraw      =   -1  'True
  100.       Height          =   2175
  101.       Index           =   2
  102.       Left            =   4560
  103.       ScaleHeight     =   -10
  104.       ScaleLeft       =   -5
  105.       ScaleMode       =   0  'User
  106.       ScaleTop        =   5
  107.       ScaleWidth      =   10
  108.       TabIndex        =   2
  109.       Top             =   0
  110.       Width           =   2175
  111.    End
  112.    Begin VB.PictureBox Pict 
  113.       AutoRedraw      =   -1  'True
  114.       Height          =   2175
  115.       Index           =   1
  116.       Left            =   2280
  117.       ScaleHeight     =   -10
  118.       ScaleLeft       =   -5
  119.       ScaleMode       =   0  'User
  120.       ScaleTop        =   5
  121.       ScaleWidth      =   10
  122.       TabIndex        =   1
  123.       Top             =   0
  124.       Width           =   2175
  125.    End
  126.    Begin VB.PictureBox Pict 
  127.       AutoRedraw      =   -1  'True
  128.       Height          =   2175
  129.       Index           =   0
  130.       Left            =   0
  131.       ScaleHeight     =   -10
  132.       ScaleLeft       =   -5
  133.       ScaleMode       =   0  'User
  134.       ScaleTop        =   5
  135.       ScaleWidth      =   10
  136.       TabIndex        =   0
  137.       Top             =   0
  138.       Width           =   2175
  139.    End
  140.    Begin VB.Label Label1 
  141.       Alignment       =   2  'Center
  142.       Caption         =   "Reverse translation"
  143.       Height          =   255
  144.       Index           =   7
  145.       Left            =   6840
  146.       TabIndex        =   15
  147.       Top             =   5040
  148.       Width           =   2175
  149.       WordWrap        =   -1  'True
  150.    End
  151.    Begin VB.Label Label1 
  152.       Alignment       =   2  'Center
  153.       Caption         =   "Reverse 1st rotation"
  154.       Height          =   255
  155.       Index           =   6
  156.       Left            =   4560
  157.       TabIndex        =   14
  158.       Top             =   5040
  159.       Width           =   2175
  160.       WordWrap        =   -1  'True
  161.    End
  162.    Begin VB.Label Label1 
  163.       Alignment       =   2  'Center
  164.       Caption         =   "Reverse 2nd rotation"
  165.       Height          =   255
  166.       Index           =   5
  167.       Left            =   2280
  168.       TabIndex        =   13
  169.       Top             =   5040
  170.       Width           =   2175
  171.       WordWrap        =   -1  'True
  172.    End
  173.    Begin VB.Label Label1 
  174.       Alignment       =   2  'Center
  175.       Caption         =   "Reflect"
  176.       Height          =   255
  177.       Index           =   4
  178.       Left            =   0
  179.       TabIndex        =   12
  180.       Top             =   5040
  181.       Width           =   2175
  182.       WordWrap        =   -1  'True
  183.    End
  184.    Begin VB.Label Label1 
  185.       Alignment       =   2  'Center
  186.       Caption         =   "Rotate into Y axis"
  187.       Height          =   255
  188.       Index           =   3
  189.       Left            =   6840
  190.       TabIndex        =   11
  191.       Top             =   2280
  192.       Width           =   2175
  193.       WordWrap        =   -1  'True
  194.    End
  195.    Begin VB.Label Label1 
  196.       Alignment       =   2  'Center
  197.       Caption         =   "Rotate into Y-Z plane"
  198.       Height          =   255
  199.       Index           =   2
  200.       Left            =   4560
  201.       TabIndex        =   10
  202.       Top             =   2280
  203.       Width           =   2175
  204.       WordWrap        =   -1  'True
  205.    End
  206.    Begin VB.Label Label1 
  207.       Alignment       =   2  'Center
  208.       Caption         =   "Translate to origin"
  209.       Height          =   255
  210.       Index           =   1
  211.       Left            =   2280
  212.       TabIndex        =   9
  213.       Top             =   2280
  214.       Width           =   2175
  215.       WordWrap        =   -1  'True
  216.    End
  217.    Begin VB.Label Label1 
  218.       Alignment       =   2  'Center
  219.       Caption         =   "Original picture"
  220.       Height          =   255
  221.       Index           =   0
  222.       Left            =   0
  223.       TabIndex        =   8
  224.       Top             =   2280
  225.       Width           =   2175
  226.       WordWrap        =   -1  'True
  227.    End
  228.    Begin VB.Menu mnuFile 
  229.       Caption         =   "&File"
  230.       Begin VB.Menu mnuFileExit 
  231.          Caption         =   "E&xit"
  232.       End
  233.    End
  234. Attribute VB_Name = "ReflectForm"
  235. Attribute VB_Creatable = False
  236. Attribute VB_Exposed = False
  237. Option Explicit
  238. ' The plane across which to reflect.
  239. Const Px = 1
  240. Const Py = 0
  241. Const Pz = 0
  242. Const Dx = 1
  243. Const Dy = 1
  244. Const Dz = 1
  245. ' Location of viewing eye.
  246. Dim EyeR As Single
  247. Dim EyeTheta As Single
  248. Dim EyePhi As Single
  249. ' Location of focus point.
  250. Const FocusX = 0#
  251. Const FocusY = 0#
  252. Const FocusZ = 0#
  253. Dim Projector(1 To 4, 1 To 4) As Single
  254. ' Matrices used for the reflection.
  255. Dim Trans(1 To 4, 1 To 4) As Single
  256. Dim Rot1(1 To 4, 1 To 4) As Single
  257. Dim Rot2(1 To 4, 1 To 4) As Single
  258. Dim Ref(1 To 4, 1 To 4) As Single
  259. Dim Rot2_Inv(1 To 4, 1 To 4) As Single
  260. Dim Rot1_Inv(1 To 4, 1 To 4) As Single
  261. Dim Trans_Inv(1 To 4, 1 To 4) As Single
  262. Dim M(0 To 7) As Transformation
  263. ' ***********************************************
  264. ' Create the matrices used when reflecting across
  265. ' a plane passing through (p1, p2, p3) with
  266. ' normal <n1, n2, n3>.
  267. ' ***********************************************
  268. Sub CreateMatrices(p1 As Single, p2 As Single, p3 As Single, n1 As Single, n2 As Single, n3 As Single)
  269. Dim D As Single
  270. Dim L As Single
  271.     ' Translate the plane to the origin.
  272.     m3Translate Trans, -p1, -p2, -p3
  273.     m3Translate Trans_Inv, p1, p2, p3
  274.     ' Rotate around Z-axis until the normal is in
  275.     ' the Y-Z plane.
  276.     m3Identity Rot1
  277.     D = Sqr(n1 * n1 + n2 * n2)
  278.     Rot1(1, 1) = n2 / D
  279.     Rot1(1, 2) = n1 / D
  280.     Rot1(2, 1) = -Rot1(1, 2)
  281.     Rot1(2, 2) = Rot1(1, 1)
  282.     m3Identity Rot1_Inv
  283.     Rot1_Inv(1, 1) = Rot1(1, 1)
  284.     Rot1_Inv(1, 2) = -Rot1(1, 2)
  285.     Rot1_Inv(2, 1) = -Rot1(2, 1)
  286.     Rot1_Inv(2, 2) = Rot1(2, 2)
  287.     ' Rotate around the X-axis until the normal
  288.     ' lies along the Y axis.
  289.     m3Identity Rot2
  290.     L = Sqr(n1 * n1 + n2 * n2 + n3 * n3)
  291.     Rot2(2, 2) = D / L
  292.     Rot2(2, 3) = -n3 / L
  293.     Rot2(3, 2) = -Rot2(2, 3)
  294.     Rot2(3, 3) = Rot2(2, 2)
  295.     m3Identity Rot2_Inv
  296.     Rot2_Inv(2, 2) = Rot2(2, 2)
  297.     Rot2_Inv(2, 3) = -Rot2(2, 3)
  298.     Rot2_Inv(3, 2) = -Rot2(3, 2)
  299.     Rot2_Inv(3, 3) = Rot2(3, 3)
  300.     ' Reflect across the X-Z plane.
  301.     m3Identity Ref
  302.     Ref(2, 2) = -1
  303.     ' Put the matrices in the M array.
  304.     m3Identity M(0).M
  305.     m3MatCopy M(1).M, Trans
  306.     m3MatCopy M(2).M, Rot1
  307.     m3MatCopy M(3).M, Rot2
  308.     m3MatCopy M(4).M, Ref
  309.     m3MatCopy M(5).M, Rot2_Inv
  310.     m3MatCopy M(6).M, Rot1_Inv
  311.     m3MatCopy M(7).M, Trans_Inv
  312. End Sub
  313. ' ***********************************************
  314. ' Let the user change the location of the eye.
  315. ' ***********************************************
  316. Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
  317. Const Dtheta = PI / 20
  318.     Select Case KeyCode
  319.         Case vbKeyLeft
  320.             EyeTheta = EyeTheta - Dtheta
  321.             
  322.         Case vbKeyRight
  323.             EyeTheta = EyeTheta + Dtheta
  324.         
  325.         Case vbKeyUp
  326.             EyePhi = EyePhi - Dtheta
  327.         
  328.         Case vbKeyDown
  329.             EyePhi = EyePhi + Dtheta
  330.         
  331.         Case Else
  332.             Exit Sub
  333.     End Select
  334.     ' Redraw the pictures.
  335.     DrawTheData
  336. End Sub
  337. ' *******************************************************
  338. ' Rotate the points in the cube and draw the cube.
  339. ' *******************************************************
  340. Private Sub DrawData(pic As Object)
  341. Dim i As Integer
  342. Dim x1 As Single
  343. Dim y1 As Single
  344. Dim x2 As Single
  345. Dim y2 As Single
  346. Dim oldwidth As Integer
  347.     ' Compute the projection matrix.
  348.     m3PProject Projector, m3Parallel, EyeR, EyePhi, EyeTheta, FocusX, FocusY, FocusZ, 0, 1, 0
  349.     ' Transform the points.
  350.     TransformAllData Projector
  351.     ' Draw the points.
  352.     pic.Cls
  353.     oldwidth = pic.DrawWidth
  354.     For i = 1 To NumSegments
  355.         x1 = Segments(i).fr_tr(1)
  356.         y1 = Segments(i).fr_tr(2)
  357.         x2 = Segments(i).to_tr(1)
  358.         y2 = Segments(i).to_tr(2)
  359.         
  360.         ' Draw the plane's normal in bold.
  361.         If i = 4 Then pic.DrawWidth = 3
  362.         pic.Line (x1, y1)-(x2, y2)
  363.         If i = 4 Then pic.DrawWidth = oldwidth
  364.     Next i
  365.     pic.Refresh
  366. End Sub
  367. Private Sub Form_Load()
  368.     ' Initialize the eye position.
  369.     EyeR = 3
  370.     EyeTheta = PI * 0.4
  371.     EyePhi = PI * 0.1
  372.     ' Create the reflection matrices.
  373.     CreateMatrices Px, Py, Pz, Dx, Dy, Dz
  374.     ' Create, project, and draw the data.
  375.     DrawTheData
  376. End Sub
  377. ' ***********************************************
  378. ' Draw all the pictures.
  379. ' ***********************************************
  380. Sub DrawTheData()
  381. Dim i As Integer
  382.     ' Start with fresh data.
  383.     CreateData
  384.     For i = 0 To 7
  385.         ' Apply the next transformation.
  386.         TransformData M(i).M, 4, NumSegments
  387.         SetPoints 4, NumSegments
  388.         
  389.         ' Display the data.
  390.         DrawData Pict(i)
  391.     Next i
  392. End Sub
  393. Sub CreateData()
  394.     ' Start with no data.
  395.     NumSegments = 0
  396.     ' Create the axes.
  397.     MakeSegment 0, 0, 0, 5, 0, 0    ' X axis.
  398.     MakeSegment 0, 0, 0, 0, 5, 0    ' Y axis.
  399.     MakeSegment 0, 0, 0, 0, 0, 5    ' Z axis.
  400.     ' Create the reflection plane's normal.
  401.     MakeSegment Px, Py, Pz, Px + Dx, Py + Dy, Pz + Dz
  402.     ' Create the edges of the reflection plane.
  403.     MakeSegment 0.5, -2, 2.5, 3.5, -2, -0.5
  404.     MakeSegment 3.5, -2, -0.5, 1.5, 2, -2.5
  405.     MakeSegment 1.5, 2, -2.5, -1.5, 2, 0.5
  406.     MakeSegment -1.5, 2, 0.5, 0.5, -2, 2.5
  407.     ' Create the object to reflect.
  408.     MakeSegment 3, 1, 2, 3, 3, 2
  409.     MakeSegment 3, 3, 2, 4, 3, 2
  410.     MakeSegment 4, 3, 2, 4, 2, 2
  411.     MakeSegment 4, 2, 2, 5, 2, 2
  412.     MakeSegment 5, 2, 2, 5, 1, 2
  413.     MakeSegment 5, 1, 2, 3, 1, 2
  414.     MakeSegment 3, 1, 1, 3, 3, 1
  415.     MakeSegment 3, 3, 1, 4, 3, 1
  416.     MakeSegment 4, 3, 1, 4, 2, 1
  417.     MakeSegment 4, 2, 1, 5, 2, 1
  418.     MakeSegment 5, 2, 1, 5, 1, 1
  419.     MakeSegment 5, 1, 1, 3, 1, 1
  420.     MakeSegment 3, 1, 1, 3, 1, 2
  421.     MakeSegment 3, 3, 1, 3, 3, 2
  422.     MakeSegment 4, 3, 1, 4, 3, 2
  423.     MakeSegment 4, 2, 1, 4, 2, 2
  424.     MakeSegment 5, 2, 1, 5, 2, 2
  425.     MakeSegment 5, 1, 1, 5, 1, 2
  426. End Sub
  427. Private Sub mnuFileExit_Click()
  428.     Unload Me
  429. End Sub
  430.